#include "doctorsindepartmentpage.h"
#include <QVBoxLayout>
#include <QTableWidget>
#include <QHeaderView>
#include <QLabel>
#include <QDebug>
//#include "doctorinfoPage.h"
#include "doctorinfopage.h"

DoctorsInDepartmentPage::DoctorsInDepartmentPage(const QString &departmentName, QWidget *parent)
    : QWidget(parent) {
    QVBoxLayout *layout = new QVBoxLayout(this);

    label = new QLabel("医生信息列表 - " + departmentName, this);
    layout->addWidget(label);

    doctorTable = new QTableWidget(this);
    doctorTable->setColumnCount(4); // 包含部门信息的列
    doctorTable->setRowCount(0); // 初始行数为0

    QStringList headers = {"医生姓名", "职称", "联系方式", "部门"};
    doctorTable->setHorizontalHeaderLabels(headers);

    tcp = new ClientTcp(DOCTORS_DATA);
    QString sql = QString("select * from Doctors where Department = '%1'").arg(departmentName);
    tcp->sendMsg(sql);

    connect(tcp->socket, &QTcpSocket::readyRead, [=]() {
        QByteArray data = tcp->socket->readAll(); // 读取所有数据

        qDebug() << "send msg";
        const QList<QJsonObject> &doctorsData = tcp->getDoctorsData();
        doctorTable->clearContents(); // 清空之前的数据
        doctorTable->setRowCount(doctorsData.size()); // 设置行数

        for (int row = 0; row < doctorsData.size(); ++row) {
            const QJsonObject &doctor = doctorsData.at(row);
            doctorTable->setItem(row, 0, new QTableWidgetItem(doctor["Name"].toString()));
            doctorTable->setItem(row, 1, new QTableWidgetItem(doctor["Title"].toString()));
            doctorTable->setItem(row, 2, new QTableWidgetItem(doctor["ContactNumber"].toString()));
            doctorTable->setItem(row, 3, new QTableWidgetItem(doctor["Department"].toString())); // 如果有部门信息

            // 将 DoctorID 存储在姓名单元格的数据中
            QTableWidgetItem *nameItem = new QTableWidgetItem(doctor["Name"].toString());
            nameItem->setData(Qt::UserRole, doctor["DoctorID"].toInt()); // 将 DoctorID 存储为用户数据
            doctorTable->setItem(row, 0, nameItem);
        }

        // 设置单元格不可编辑
        for (int row = 0; row < doctorTable->rowCount(); ++row) {
            for (int column = 0; column < doctorTable->columnCount(); ++column) {
                QTableWidgetItem *item = doctorTable->item(row, column);
                if (item) {
                    item->setFlags(item->flags() & ~Qt::ItemIsEditable);
                }
            }
        }
    });

    // 设置点击事件
    connect(doctorTable, &QTableWidget::cellClicked, this, [this](int row, int column) {
        if (column == 0) { // 点击的是医生姓名列
            int doctorId = doctorTable->item(row, column)->data(Qt::UserRole).toInt();
            openDoctorInfoPage(doctorId);
        }
    });

    doctorTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 自适应列宽
    layout->addWidget(doctorTable);
    setLayout(layout);
}

void DoctorsInDepartmentPage::openDoctorInfoPage(int doctorId) {
    DoctorInfoPage *infoPage = new DoctorInfoPage(doctorId);
    infoPage->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动删除
    infoPage->show(); // 显示医生信息页面
}

